// 人脸识别视图

//var $pic1, $pic2, $pic3;
var $verifyBtn, $phone, $verification_code, $name, $frId, $agreeInput;
var daojs = 60;
var stepNum=0;
var stepTimer;
var $steps;
var idRegExp;
var tempName,tempId,temPhone,tempCode,tempVerifyBtn;
/*
import{
    idRegExp
} from '../components/commonRegExp.js';*/
import {
    titileInputInit
} from '../components/titleInput.js';

function viewInit(argument) {
    initVal();
    infoInit();
    
    titileInputInit();
    bindViewEvent();
    $("#fanhui").on("touchstart", function() {
        window.history.back(); 
    });
    


}

// 验证码倒计时
function djsfunc() {
    var handle = setInterval(function() {
        $verifyBtn.text("重新获取(" + daojs + "S)");
        daojs--;
        if (daojs == 0) {
            clearInterval(handle);
            $verifyBtn.attr("weui-btn weui-btn_plain-primary");
            $verifyBtn.text("重新获取验证码");
            daojs = 60;
            $verifyBtn.attr("class", "weui-btn weui-btn_plain-primary");
        }
    }, 1000);
}

// 验证输入框 是否允许提交
function checkedInputValue() {
    var nameVal = $name.val();
    var frIdVal = $frId.val();
    var nameLength = len(nameVal);
    var verifyCode = $verification_code.val();
    if ($phone.val().length == 11 && verifyCode.length == 4 && nameLength > 2 && idRegExp.test(frIdVal) && $agreeInput.attr("checked")) {
        $("#submit_btn").attr("class", "weui-btn weui-btn_primary");
    } else {
        $("#submit_btn").attr("class", "weui-btn weui-btn_disabled weui-btn_default ui-link");
    }
}

// 计算姓名长度
function getNameLength() {
    var nameVal = $name.val();
    var nameLength = len(nameVal);
    return nameLength;
}

//取字符串长度，中文算2位
function len(s) {
    var l = 0;
    var a = s.split("");
    for (var i = 0; i < a.length; i++) {
        if (a[i].charCodeAt(0) < 299) {
            l++;
        } else {
            l += 2;
        }
    }
    return l;
}

// 
function idInput() {
    var idVal = $frId.val();
    idVal = idVal.replace(/\D/g, "X")
    $frId.val(idVal);
}

// 

// 绑定事件
function bindViewEvent() {
    $("#phone").on("input", function() {
        var oldPhone = $(this).val();
        if (oldPhone.length == 11) {
            $verifyBtn.attr("class", "weui-btn weui-btn_plain-primary");
        } else {
            $verifyBtn.attr("class", "weui-btn weui-btn_plain-default weui-btn_plain-disabled");
        }
    });

    $(".weui-cells_form").on("input", checkedInputValue);
    $frId.on("input", idInput);
    $("#checkButton").on("click",function  () {
        if($agreeInput.attr("checked")){
        	 $(".icheckbox-blue").removeClass("checked");
            $agreeInput.attr("checked","");
            $agreeInput.removeAttr("checked");

        }else{
            $(".icheckbox-blue").addClass("checked");
            $agreeInput.attr("checked","");
        }
        checkedInputValue();
          
    });
    $("#contract").on("click",function  () {
    	tempInfo();
    	location.href="./contract.html";
    });
}




function initVal() {
    $steps=$(".step-box").children();
    $verifyBtn = $("#verifyBtn");
    $phone = $("#phone");
    $verification_code = $("#verification_code");
    $name = $("#name");
    $agreeInput = $("#checkButton").find("input");
    $frId = $("#frId");
   
    idRegExp = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/;

}

/*duhui 初始化第二步 */

function initStep2View(){
	stepTimer=setInterval(moveNext,1500);
}
/*步骤显示*/
function moveNext() {
	$steps.eq(stepNum).fadeIn();
	stepNum++;
	if(stepNum == 5) {
		
		$('#recording').fadeIn();
		$('#recording').css({"display":"block"});
		clearInterval(stepTimer);
	}
}



// 设置验证结算
function setStepLoaderEnd(stepVal) {
    var selector_loader = "#step3 .step_" + stepVal + " .loader-inner";
    var selector_duiguo = "#step3 .step_" + stepVal + " .duiguo_parent";
    $(selector_loader).hide();
    $(selector_duiguo).show();
}
// 正在验证查询第几步骤
function setStepLoader(stepVal) {
    var selector_wait = "#step3 .step_" + stepVal + " .step3_wait";
    var selector_loader = "#step3 .step_" + stepVal + " .loader-inner";
    $(selector_wait).hide();
    $(selector_loader).show();
}

//临时存储输入框信息　便于用户返回时不用重新填
function tempInfo(){
	sessionStorage.setItem("tempName",$("#name").val());
	sessionStorage.setItem("tempId",$("#frId").val());
	sessionStorage.setItem("temPhone",$("#phone").val());
	sessionStorage.setItem("tempCode",$("#verification_code").val());
	
		
}
//进入页面初始化
function infoInit(){
	if(sessionStorage.getItem("tempName")!="undefind"){
		setTempInfo();
		if ($("#phone").val().length == 11) {
            $verifyBtn.attr("class", "weui-btn weui-btn_plain-primary");
        } else {
            $verifyBtn.attr("class", "weui-btn weui-btn_plain-default weui-btn_plain-disabled");
        }
        checkedInputValue();
		deleteTempInfo();
	}
	
	
}
//设值输入框的值
function setTempInfo(){
	tempName=sessionStorage.getItem("tempName");
	$("#name").val(tempName);
	tempId=sessionStorage.getItem("tempId");
	$("#frId").val(tempId);
	temPhone=sessionStorage.getItem("temPhone");
	$("#phone").val(temPhone);
	tempCode=sessionStorage.getItem("tempCode");
	$("#verification_code").val(tempCode);
	
}
//删除存储中的数据
function deleteTempInfo(){
	sessionStorage.removeItem("tempName");
	sessionStorage.removeItem("tempId");
	sessionStorage.removeItem("temPhone");
	sessionStorage.removeItem("tempCode");
	sessionStorage.removeItem("tempVerifyBtn");
	
}




export {
    viewInit,
    djsfunc,
    /*setDemoPic,
    setRoundLeft,*/
    setStepLoader,
    setStepLoaderEnd,
    initStep2View
}